package com.sam.mvp.ui.adapter;

import android.widget.TextView;

import com.chad.library.adapter.base.BaseViewHolder;
import com.crazysunj.multitypeadapter.adapter.LoadingEntityAdapter;
import com.crazysunj.multitypeadapter.helper.LoadingConfig;
import com.crazysunj.multitypeadapter.helper.RecyclerViewAdapterHelper;
import com.crazysunj.multitypeadapter.util.IDUtil;
import com.sam.R;
import com.sam.base.BaseAdapter;
import com.sam.base.MutiTypeTitleEntity;
import com.sam.mvp.entity.CommonFooterEntity;
import com.sam.mvp.entity.CommonHeadEntity;
import com.sam.mvp.entity.HomeEnity;
import com.sam.mvp.entity.LoadingEntity;
import com.sam.mvp.helper.HomeAdapterHelper;

import java.util.LinkedHashMap;
import java.util.List;

/**
 * @ProjectName: SamLib
 * @Package: com.sam.mvp.ui.adapter
 * @ClassName: HomeAdapter
 * @Description:
 * @Author: Carl
 * @CreateDate: 2018/10/24 11:50
 * @UpdateUser:
 * @UpdateDate: 2018/10/24 11:50
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public class HomeAdapter extends BaseAdapter<MutiTypeTitleEntity, BaseViewHolder, HomeAdapterHelper> {

    protected LinkedHashMap<Integer, MutiTypeTitleEntity> headerMap = new LinkedHashMap<>();
    protected LinkedHashMap<Integer, MutiTypeTitleEntity> footerMap = new LinkedHashMap<>();


    public HomeAdapter() {
        super(new HomeAdapterHelper());

        headerMap.put(HomeEnity.TOP, new CommonHeadEntity(HomeEnity.TOP_HEAD_TITLE, HomeEnity.TOP));
        headerMap.put(HomeEnity.CENTER, new CommonHeadEntity(HomeEnity.CENTER_HEAD_TITLE, HomeEnity.CENTER));
        headerMap.put(HomeEnity.BOTTOM, new CommonHeadEntity(HomeEnity.BOTTOM_HEAD_TITLE, HomeEnity.BOTTOM));

        footerMap.put(HomeEnity.TOP, new CommonFooterEntity(HomeEnity.TOP_FOOTER_TITLE, HomeEnity.TOP));
        footerMap.put(HomeEnity.CENTER, new CommonFooterEntity(HomeEnity.CENTER_FOOTER_TITLE, HomeEnity.CENTER));
        footerMap.put(HomeEnity.BOTTOM, new CommonFooterEntity(HomeEnity.BOTTOM_FOOTER_TITLE, HomeEnity.BOTTOM));


        mHelper.setLoadingAdapter(new LoadingEntityAdapter<MutiTypeTitleEntity>() {
            @Override
            public MutiTypeTitleEntity createLoadingEntity(int type, int level) {
                return new LoadingEntity(type, IDUtil.getId());
            }

            @Override
            public MutiTypeTitleEntity createLoadingHeaderEntity(int type, int level) {
                return new LoadingEntity(type, IDUtil.getId());
            }

            @Override
            public void bindLoadingEntity(MutiTypeTitleEntity loadingEntity, int position) {

            }
        });

        mHelper.initGlobalLoadingConfig(new LoadingConfig.Builder()
                .setLoading(HomeAdapterHelper.TOP, 2, true)
                .setLoading(HomeAdapterHelper.CENTER, 2)
                .setLoading(HomeAdapterHelper.BOTTOM, 2)
                .build());
    }

    @Override
    protected void convert(BaseViewHolder helper, MutiTypeTitleEntity item) {
        int itemType = item.getItemType();
        switch (itemType) {
            case HomeEnity.TOP:
                break;
            case HomeEnity.CENTER:
                break;
            case HomeEnity.BOTTOM:
                break;
            case HomeAdapterHelper.TOP - RecyclerViewAdapterHelper.HEADER_TYPE_DIFFER:
            case HomeAdapterHelper.CENTER - RecyclerViewAdapterHelper.HEADER_TYPE_DIFFER:
            case HomeAdapterHelper.BOTTOM - RecyclerViewAdapterHelper.HEADER_TYPE_DIFFER:
                helper.setText(R.id.title, item.getTitle());
                break;
            case HomeAdapterHelper.TOP - RecyclerViewAdapterHelper.FOOTER_TYPE_DIFFER:
                helper.setText(R.id.item_footer, item.getTitle());
                final int type = item.getItemType() + RecyclerViewAdapterHelper.FOOTER_TYPE_DIFFER;
                final TextView footer = helper.getView(R.id.item_footer);
                footer.setText(item.getTitle());
                footer.setOnClickListener(v -> {
                    if (HomeEnity.TOP_FOOTER_TITLE.equals(item.getTitle())) {
                        item.setTitle("收起来");
                        mHelper.foldType(type, false);
                    } else {
                        item.setTitle(HomeEnity.TOP_FOOTER_TITLE);
                        mHelper.foldType(type, true);
                    }
                    footer.setText(item.getTitle());
                });
                break;

        }
    }


    public void notifyLoading() {
        mHelper.notifyLoadingChanged();
    }

    public void release() {
        mHelper.release();
        headerMap = null;
        footerMap = null;
    }

    public void notityDataAndHeadAndFooter(List<HomeEnity> items, String headTitle, String footerTitle, int level) {
        mHelper.notifyModuleDataAndHeaderAndFooterChanged(
                items,
                headerMap.get(level),
                footerMap.get(level),
                level);
    }

    public void notifyDataAndHead(List<HomeEnity> items, String headTitle, int level) {
        mHelper.notifyModuleDataAndHeaderChanged(
                items,
                headerMap.get(level),
                level);
    }

    public void notifyDataAndFooter(List<HomeEnity> items, String footerTitle, int level) {
        mHelper.notifyModuleDataAndFooterChanged(
                items,
                footerMap.get(level),
                level);
    }

    public void notifyData(List<HomeEnity> items, int level) {
        mHelper.notifyModuleDataChanged(items, level);
    }


}
